package com.zigong.app.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.Toast;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.zigong.app.R;
import com.zigong.app.base.BaseActivity;
import com.zigong.app.beans.HomeListItem;
import com.zigong.app.beans.NewsItem;
import com.zigong.app.ui.activities.VideoActivity;
import com.zigong.app.ui.adapter.HomeHotVideoContentAdapter;
import com.zigong.app.ui.adapter.HomeTopNewsListAdapter;
import com.zigong.app.ui.utils.SkipUtil;
import com.zigong.app.ui.utils.UiListMarginUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeTopNewsListHolder extends BaseAbsHolder<HomeListItem> {


    @BindView(R.id.item_home_news_list)
    protected RecyclerView news_list;
    @BindView(R.id.top_divider)
    protected View topDivider;

    private HomeTopNewsListAdapter topNewsAdapter;
    private int number;

    public HomeTopNewsListHolder(Context context, View itemView,int n) {
        super(context, itemView);
        number = n;
    }

    @Override
    public void setData(HomeListItem newsItem) {
        init(newsItem.getNewsItemList());
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    private void init(List<NewsItem> list) {
        int pa = context.getResources().getDimensionPixelOffset(R.dimen.item_list_margin_bottom);
//        if (list.size() == 1) {
//            video_list.setPadding(0, 0, 0, pa);
//        } else {
//        news_list.setPadding(pa, 0, pa, pa);
//        }
        if(number == 1) {
            topDivider.setVisibility(View.GONE);
        }else{
            topDivider.setVisibility(View.VISIBLE);
        }
        LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        news_list.setLayoutManager(manager);
        news_list.setItemAnimator(new DefaultItemAnimator());
        if (topNewsAdapter == null) {
            topNewsAdapter = new HomeTopNewsListAdapter(context, list).setItemInternalClick(onHotVideoClick);
            news_list.setAdapter(topNewsAdapter);
        } else {
            topNewsAdapter.setData(list);
        }

    }

    OnItemInternalClick onHotVideoClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            String title = topNewsAdapter.getItem(position).getNewsTitle();
            ArrayList<Pair<String, String>> pairs = new ArrayList<>();
            pairs.add(new Pair<>("页面", "首页"));
            pairs.add(new Pair<>("栏目", "精选"));
            pairs.add(new Pair<>("位置", "热门视频"));
            ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
            tempPair.addAll(pairs);
            tempPair.add(new Pair<>("行为类型", "点击"));
//            pairs.add(new Pair<>("行为类型", "点击"));
            if (activity != null) {
                GridsumWebDissector.getInstance().trackEvent(activity, "", title, "", 200, tempPair);
            } else if (fragment != null) {
                GridsumWebDissector.getInstance().trackEvent(fragment, "", title, "", 200, tempPair);
            }
            HashMap<String, String> map = new HashMap<>();
            for (Pair<String, String> p : pairs) {
                map.put(p.first, p.second);
            }
            SkipUtil.skipWithNewsItem(context, topNewsAdapter.getItem(position), map);
        }
    };

//    @OnClick(value = {R.id.home_item_title_more})
//    public void MoreClick(View view) {
//        Intent intent = new Intent(context, VideoActivity.class);
//        if (!TextUtils.isEmpty(homeListItem.getSubUrl()) && homeListItem.getSubUrl().endsWith(".json")) {
//            ArrayList<Pair<String, String>> pairs = new ArrayList<>();
//            pairs.add(new Pair<>("页面", "首页"));
//            pairs.add(new Pair<>("栏目", "精选"));
//            pairs.add(new Pair<>("位置", "热门视频_更多"));
//            pairs.add(new Pair<>("行为类型", "点击"));
//            if (activity != null) {
//                GridsumWebDissector.getInstance().trackEvent(activity, "", "热门视频_更多", "", 200, pairs);
//            } else if (fragment != null) {
//                GridsumWebDissector.getInstance().trackEvent(fragment, "", "热门视频_更多", "", 200, pairs);
//            }
//            intent.putExtra(BaseActivity.EX_HTML, homeListItem.getSubUrl());
//            context.startActivity(intent);
//        } else {
//            Toast.makeText(context, "没有更多热门视频", Toast.LENGTH_SHORT).show();
//        }
//    }
}
